This notebook summarizes the realtime-kinetic measurements.
Before running this notebook, you need to pre-process the data with:
This pre-processing analyzes the 3 measurement data files, compute the moving-window slices, the number of bursts and fits the population fractions. All results are saved as TXT in the results folder.
The present notebook loads these results and presents a summary.
In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
In [2]:
import time
start_time = time.time()
time.ctime()
Out[2]:
In [3]:
import analysis
In [4]:
import models
In [5]:
filenames = ['multispot_2015-07-%d_'+f for f in [
'bubble-bubble-run-off-kinetics-800mW-steer110_12',
'bubble-bubble-open-complex-run-off-kinetics-600mW-steer110_7',
'bubble-bubble-run-off-kinetics-800mW-steer110_8']]
filenames = [f % d for f, d in zip(filenames, (31, 29, 30))]
filenames
Out[5]:
In [6]:
meas_id = 0
fitres, params = analysis.process(filenames[meas_id], post = (600, 1700), post2_start=2200)
In [7]:
res, resw, rest0f, reswt0f, ci, ciw = fitres
In [8]:
res[30].best_values
Out[8]:
In [9]:
import lmfit
In [10]:
res[30].best_values
Out[10]:
In [11]:
resw[30].best_values
Out[11]:
In [12]:
methods, windows, step = analysis._get_methods_windows_step(filenames[0])
In [13]:
[res[k].redchi for k in sorted(res)]
Out[13]:
In [14]:
[resw[k].redchi for k in sorted(resw)]
Out[14]:
In [15]:
tau = {k: r.best_values['tau'] for k, r in res.items()}
tauw = {k: r.best_values['tau'] for k, r in resw.items()}
In [16]:
tau
Out[16]:
In [17]:
tauw
Out[17]:
In [18]:
p = {window: params['em', window, 1] for window in windows}
In [19]:
def gauss3():
peak1 = lmfit.models.GaussianModel(prefix='p1_')
peak3 = lmfit.models.GaussianModel(prefix='p3_')
peak2 = lmfit.models.GaussianModel(prefix='p2_')
model = peak1 + peak2 + peak3
model.set_param_hint('p1_center', value=0, min=0, max=0.2)
model.set_param_hint('p2_center', value=0.5, min=0, max=1)
model.set_param_hint('p3_center', value=0.9, min=0.8, max=1)
model.set_param_hint('p1_sigma', value=0.02, min=0.01, max=0.2)
model.set_param_hint('p2_sigma', value=0.02, min=0.01, max=0.2)
model.set_param_hint('p3_sigma', value=0.02, min=0.01, max=0.1)
model.set_param_hint('p1_amplitude', value=1, min=0.01)
model.set_param_hint('p2_amplitude', value=1, min=0.01)
model.set_param_hint('p3_amplitude', value=1, min=0.01)
model.name = '3 gauss peaks'
return model
In [20]:
model = gauss3()
In [21]:
px = p[60]
In [22]:
p_names = ('p1_center', 'p1_sigma',
'p2_amplitude', 'p2_center', 'p2_sigma',
'p3_amplitude', 'p3_center', 'p3_sigma')
params = px.loc[:, p_names]
params['p1_amplitude'] = 1 - params.p2_amplitude - params.p3_amplitude
In [23]:
E = np.arange(-0.1, 1.11, 0.001)
time = np.arange(-0, 530, 30)
c = sns.color_palette('RdYlBu', len(time))
sns.palplot(c)
In [24]:
sns.set(font_scale=1.4)
with sns.axes_style("ticks"):
fig, ax = plt.subplots(figsize=(6, 4))
for i, t in enumerate(time):
y_dist = model.eval(x=E, **dict(params.loc[t]))
plt.plot(E, y_dist,
lw=4, alpha=1, color=c[i % len(c)], label='%d s' % t)
plt.xlim(0.2)
#plt.ylim(0, 4)
plt.xlabel('E')
plt.ylabel('Probability (A.U.)')
ax.text(0.4, 1.6, 'time', fontsize=18)
#ax.arrow(0.5, 1.5, 0.2, -1, head_width=0.1, head_length=0.1, overhang=0.2, lw=2, fc='k', ec='k')
ax.annotate('', xy=(0.6, 0.2,), xytext=(0.4, 1.5), size=15,
arrowprops=dict(
arrowstyle="simple,head_length=0.8,head_width=0.8,tail_width=0.2",
#arrowstyle="->,head_length=0.8,head_width=0.4",
connectionstyle="arc3,rad=-0.1"),
color=1,)
#ax.yaxis.set_visible(False)
sns.despine(fig, ax, trim=True, offset=10, left=False)
#plt.legend()
plt.savefig('figures/paper_fig_rk_E_hist.png', dpi=200, bbox_inches='tight')
In [25]:
quenchk = pd.DataFrame.from_csv('quenched_kinetics.txt')
quenchk.index.name = 'time'
quenchk.columns=['kinetics']
quenchk.T
Out[25]:
For visualization, normalize quenched kinetics data to same initial and final values as realtime kinetics:
In [26]:
qk_init, qk_final = float(quenchk.iloc[0]), float(quenchk.iloc[-1])
qk_init, qk_final
Out[26]:
In [27]:
w = windows[1]
rk_init = resw[w].best_values['init_value']
rk_final = resw[w].best_values['final_value']
w, rk_init, rk_final
Out[27]:
In [28]:
quenchk['norm_kinetics'] = ((quenchk.kinetics - qk_init)/(qk_final - qk_init)
*(rk_final - rk_init) + rk_init)
In [29]:
model = models.factory_model_exp()
In [30]:
qk_res = model.fit(np.array(quenchk.kinetics), t=quenchk.index)
In [31]:
qk_res.best_values
Out[31]:
In [32]:
w, wshort = 30, 5
tau_str = (r'$\tau_{%ds} = %.1f s (%.1f, %.1f)$' % (w, tauw[w], ciw[w]['tau'][2][1], ciw[w]['tau'][4][1]))
sns.set(font_scale=1.4)
with sns.axes_style("ticks"):
fig, ax = plt.subplots(figsize=(12, 4))
ax.set_xlim(-630, 1730)
ax.plot('tmean', 'kinetics', 'o', alpha=0.2, color='grey', data=p[wshort], label='')
ax.plot('tmean', 'kinetics', 'o', alpha=1, color=analysis.blue, data=p[w], label='')
ax.plot(p[w].tstart, models.expwindec_func(p[w].tstart, **resw[w].best_values),
color='k', label=tau_str)
#ax.legend(loc='lower right', fontsize=18);
ax.plot('norm_kinetics', data=quenchk, ls='', marker='s', color='brown')
ax.set_xlabel('Time (s)')
ax.legend(loc='lower right',
labels=['(a) 5s Integration', '(b) 30s Integration', 'Fit of (b)', 'Quenched Kinetics'],
fontsize=14)
ax.text(-80, 0.7, '+NTP', fontsize=18)
ax.arrow(0, 0.65, 0, -0.25, head_width=30, head_length=0.1, overhang=0.2, lw=2, fc='k', ec='k')
ax.set_yticks(np.arange(0, 1, 0.2))
sns.despine(fig, ax, trim=True, offset=5)
plt.savefig('figures/paper_fig_kinetics.png', dpi=200, bbox_inches='tight')
In [33]:
w, wshort = 60, 5
tau_str = (r'$\tau_{%ds} = %.1f s (%.1f, %.1f)$' % (w, tau[w], ci[w]['tau'][2][1], ci[w]['tau'][4][1]))
fig, ax = plt.subplots(figsize=(12, 4))
ax.set_xlim(-600, 1700)
ax.plot('tstart', 'kinetics', 'o', alpha=0.2, color='grey', data=p[wshort], label='')
ax.plot('tstart', 'kinetics', 'o', alpha=0.5, color=analysis.blue, data=p[w], label='')
ax.plot(p[w].tstart, models.expwindec_func(p[w].tstart, **resw[w].best_values),
color='k', label=tau_str)
ax.legend(loc='lower right', fontsize=18)
Out[33]:
In [34]:
index = pd.MultiIndex.from_product([(0, 1, 2), windows, (True, False), (True, False)],
names=['meas_id', 'window', 'integr', 't0_vary'])
index
Out[34]:
In [35]:
fitdata = pd.DataFrame(index=index, columns=['tau', 't0', 'init_value', 'final_value', 'redchi'], dtype=float)
fitdata = fitdata.sort_index()
#fitdata.head()
In [36]:
meas_id
Out[36]:
In [37]:
for i, w in enumerate(windows):
fitdata.loc[meas_id, w, False, True] = {**res[w].best_values, 'redchi': res[w].redchi*1e3}
fitdata.loc[meas_id, w, False, False] = {**rest0f[w].best_values, 'redchi': rest0f[w].redchi*1e3}
fitdata.loc[meas_id, w, True, True] = {**resw[w].best_values, 'redchi': resw[w].redchi*1e3}
fitdata.loc[meas_id, w, True, False] = {**reswt0f[w].best_values, 'redchi': reswt0f[w].redchi*1e3}
In [38]:
fitdata.head()
Out[38]:
In [39]:
assert (fitdata.loc[pd.IndexSlice[meas_id, :, :, False], 't0'] == 0).all()
In [40]:
meas_id = 1
fitres, params = analysis.process(filenames[meas_id], post = (600, 1500), post2_start=2200)
In [41]:
params = analysis.load_fit_data(filenames[meas_id])
In [42]:
kin = (-600, 1600)
p1 = params['em', 5, 1]
p2 = params['em', 30, 1]
p3 = params['em', 60, 1]
p2.index = p2.tstart
p2k = p2.loc[kin[0]:kin[1]]
p3.index = p3.tstart
p3k = p3.loc[kin[0]:kin[1]]
In [43]:
methods, windows, step = analysis._get_methods_windows_step(filenames[1])
decimation = 5
method = 'nelder'
windows
Out[43]:
In [44]:
t0_vary = True
modelw3 = models.factory_model_expwin(tau=150, t_window=windows[2], decimation=decimation, t0_vary=t0_vary)
resw3 = modelw3.fit(np.array(p3k.kinetics), t=p3k.tstart, verbose=False, method=method)
resw3 = modelw3.fit(np.array(p3k.kinetics), t=p3k.tstart, verbose=False)
resw3.best_values
Out[44]:
In [45]:
t0_vary = True
modelw2 = models.factory_model_expwin(tau=150, t_window=windows[1], decimation=decimation, t0_vary=t0_vary)
resw2 = modelw2.fit(np.array(p2k.kinetics), t=p2k.tstart, verbose=False, method=method)
resw2 = modelw2.fit(np.array(p2k.kinetics), t=p2k.tstart, verbose=False)
resw2.best_values
Out[45]:
In [46]:
fig_width = 14
fig, ax = plt.subplots(figsize=(fig_width, 4))
#ax.plot('tstart', 'kinetics', 'o', alpha=0.2, color='grey', data=p1)
ax.plot('tstart', 'kinetics', 'o', alpha=0.5, data=p2k)
ax.plot('tstart', 'kinetics', 'o', alpha=0.5, data=p3k)
ax.plot(p2k.tstart-15, models.expwindec_func(p2k.tstart, **resw2.best_values), 'm')
ax.plot(p3k.tstart, models.expwindec_func(p3k.tstart, **resw3.best_values), 'k')
ax.set_xlim(kin[0], kin[1])
# ax.text(kin[0]+50, 0.62, r'$\tau_{5s} = %.1f s (%.1f, %.1f)$' % (tauw1, ciw1['tau'][2][1], ciw1['tau'][4][1]), fontsize=18)
#ax.text(kin[0]+50, 0.52, r'$\tau_{30s} = %.1f s (%.1f, %.1f)$' % (tauw2, ciw2['tau'][2][1], ciw2['tau'][4][1]), fontsize=18)
# title = 'Kinetics Fit - Intergrated Exponential (t0_vary=%s)' % t0_vary
# ax.set_title(title, fontsize=fs)
Out[46]:
In [47]:
meas_id
Out[47]:
In [48]:
methods, windows, step = analysis._get_methods_windows_step(filenames[meas_id])
res, resw, rest0f, reswt0f, ci, ciw = fitres
for i, w in enumerate(windows):
fitdata.loc[meas_id, w, False, True] = {**res[w].best_values, 'redchi': res[w].redchi*1e3}
fitdata.loc[meas_id, w, False, False] = {**rest0f[w].best_values, 'redchi': rest0f[w].redchi*1e3}
fitdata.loc[meas_id, w, True, True] = {**resw[w].best_values, 'redchi': resw[w].redchi*1e3}
fitdata.loc[meas_id, w, True, False] = {**reswt0f[w].best_values, 'redchi': reswt0f[w].redchi*1e3}
In [49]:
assert (fitdata.loc[pd.IndexSlice[meas_id, :, :, False], 't0'] == 0).all()
In [50]:
meas_id = 2
fitres, params = analysis.process(filenames[meas_id], post = (600, 1500), post2_start=2200)
In [51]:
meas_id
Out[51]:
In [52]:
methods, windows, step = analysis._get_methods_windows_step(filenames[meas_id])
res, resw, rest0f, reswt0f, ci, ciw = fitres
for i, w in enumerate(windows):
fitdata.loc[meas_id, w, False, True] = {**res[w].best_values, 'redchi': res[w].redchi*1e3}
fitdata.loc[meas_id, w, False, False] = {**rest0f[w].best_values, 'redchi': rest0f[w].redchi*1e3}
fitdata.loc[meas_id, w, True, True] = {**resw[w].best_values, 'redchi': resw[w].redchi*1e3}
fitdata.loc[meas_id, w, True, False] = {**reswt0f[w].best_values, 'redchi': reswt0f[w].redchi*1e3}
In [53]:
assert (fitdata.loc[pd.IndexSlice[meas_id, :, :, False], 't0'] == 0).all()
In [54]:
fitdata.sortlevel(0, axis=0, inplace=True)
In [55]:
df = fitdata.xs((False, True), level=('integr', 't0_vary'))
dfw = fitdata.xs((True, True), level=('integr', 't0_vary'))
In [56]:
#fitdata.loc[(slice(None), slice(30,60), True, True), ]
In [57]:
dfw - df
Out[57]:
In [58]:
fitdata = fitdata.round({'tau': 1, 't0': 1, 'init_value': 3, 'final_value': 3, 'redchi': 3})
fitdata.head()
Out[58]:
In [59]:
fitdata.to_csv('results/8-spot-bubble-bubble-fitdata.csv')
In [60]:
import time
print('Execution duration: %d s' % (time.time() - start_time))